package com.yueya.business.qa;

import java.util.HashMap;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.yueya.business.qa.model.Qa;
import com.yueya.common.action.BaseController;
import com.yueya.common.model.Json;
import com.yueya.common.util.DateFormat;
import com.yueya.common.util.UUIDGenerator;

public class QaController extends BaseController {

	Map<String, String> map = new HashMap<String, String>();

	public void index() {
		render("/WEB-INF/views/business/qa/qa_list.jsp");
	}

	@SuppressWarnings("rawtypes")
	public void getAllListPager() {
		map.clear();
		map.put("name", this.getPara("name"));
		Page page = Qa.service.pagination(this.getPage(), this.getRows(), map);
		jsonMap.put("total", page.getTotalRow());
		jsonMap.put("rows", page.getList());
		renderJson(JsonKit.toJson(jsonMap));

	}

	public void doAdd() {
		render("/WEB-INF/views/business/qa/qa_edit.jsp");
	}

	public void doEdit() {
		Qa qa = Qa.dao.findById(this.getPara("id"));
		this.setAttr("qa", qa);
		this.setAttr("visit", PropKit.get("upload.visit"));
		render("/WEB-INF/views/business/qa/qa_edit.jsp");
	}

	// 操作 保存
	public void save() {
		boolean bl = false;
		Qa qa = this.getModel(Qa.class);
		if (StrKit.notBlank(qa.getStr("id"))) {
			qa.set("update_time", DateFormat.getDate("yyyy-MM-dd HH:mm:ss"));
			bl = qa.update();
		} else {
			qa.set("id", UUIDGenerator.getUUID());
			qa.set("create_time", DateFormat.getDate("yyyy-MM-dd HH:mm:ss"));
			bl = qa.save();
		}
		Json j = new Json();
		if (bl) {
			j.setSuccess(true);
			j.setMsg("提交成功");
		} else {
			j.setSuccess(false);
			j.setMsg("提交失败");
		}
		renderJson(j);
	}

	// 删除
	@Before(Tx.class)
	public void remove() {
		boolean bl = false;
		if (getPara("id") != null) {
			String id = this.getPara("id").replace("/", "").trim();
			bl = Qa.service.remove(id);
		}
		Json j = new Json();
		if (bl) {
			j.setSuccess(true);
			j.setMsg("删除成功!");
		} else {
			j.setSuccess(false);
			j.setMsg("删除失败!");
		}
		this.renderJson(j);
	}

	// 批量移除
	@Before(Tx.class)
	public void bathremove() {
		Boolean bl = false;
		String ids = this.getPara("ids");
		if (ids != null) {
			bl = Qa.service.bathremove(ids);
		}
		Json j = new Json();
		if (bl == true) {
			j.setSuccess(true);
			j.setMsg("删除成功!");
		} else {
			j.setSuccess(false);
			j.setMsg("删除失败!");
		}
		this.renderJson(j);
	}

}
